/**
 * ParticleGenerator
 * 
 **/
var ParticleGenerator = function() {
	this.entities = [];
	this.origine = null;

	// INITIALIZATION ---
	this.fire = function(origine) {
		var colors = [
			"rgba(255,234,196,.8)",
			"rgba(255,220,157,.8)",
			"rgba(255,207,118,.8)",
			"rgba(255,193,78,.8)",
			"rgba(255,179,39,.8)",
			"rgba(255,165,0,.8)",
			"rgba(216,140,0,.8)",
			"rgba(177,114,0,.8)",
			"rgba(137,89,0,.8)"
		];
		var cE = Math.pow(origine.size, 2);

		//Constante de l'angle
		var N = ((Math.PI*2) / cE)
		for(var n = 0; n < cE; n++) {

			//Variable selon le n
			var angle = N * n - Math.PI/2;
			var x = rand(0, Math.sin(angle) * origine.size);
			var y = rand(0, Math.cos(angle) * origine.size);

			var entity = {
				c : colors.pickup(),
				x : x,
				y : y,
				xs: 5,
				ys: 5,
				s : 2,
				l : origine.distance
			};
			this.entities.push(entity);
		}

		this.origine = origine;
		this.toExplode = false;
	}

	this.ice = function(origine) {
		var colors = [
			"rgba(235,255,255,.8)",
			"rgba(196,255,255,.8)",
			"rgba(157,255,255,.8)",
			"rgba(118,255,255,.8)",
			"rgba(78,255,255,.8)",
			"rgba(39,255,255,.8)",
			"rgba(0,255,255,.8)",
			"rgba(0,216,216,.8)",
			"rgba(0,177,177,.8)"
		];
		var cE = Math.pow(origine.size, 2);

		//Constante de l'angle
		var N = ((Math.PI*2) / cE)
		for(var n = 0; n < cE; n++) {

			//Variable selon le n
			var angle = N * n - Math.PI/2;
			var x = rand(0, Math.sin(angle) * origine.size);
			var y = rand(0, Math.cos(angle) * origine.size);

			var entity = {
				c : colors.pickup(),
				x : x,
				y : y,
				xs: 5,
				ys: 5,
				s : 2,
				l : origine.distance
			};
			this.entities.push(entity);
		}

		this.origine = origine;
		this.toExplode = false;
	}
	// --- END INITIALIZATION

	this.update = function() {
		for(var iE = 0, cE = this.entities.length; iE < cE; iE++) {
			var e = this.entities[iE];
			if(this.toExplode == true) {
				e.x -= rand(0, e.xs);
				e.y -= rand(0, e.ys);
			}

			e.l --;
		}
	}

	this.render = function(){
		for(var iE = 0, cE = this.entities.length; iE < cE; iE++) {
			var e = this.entities[iE];
			CTX.fillStyle = e.c;
			CTX.fillRect(e.x, e.y, e.s, e.s);
		}
		CTX.globalAlpha = 1;
	};

	this.explode = function() {
		for(var iE = 0, cE = this.entities.length; iE < cE; iE++) {
			var e = this.entities[iE];
			e.xs = rand(-e.xs, e.xs)*5;
			e.ys = rand(-e.ys, e.ys)*5;
			// Blood option
			// e.c = "rgba(255,0,0,"+rand(0,.5)+")";
		}
		this.toExplode = true;
	}

	this.destroy = function() {
		for(var iE = 0, cE = this.entities.length; iE < cE; iE++) {
			this.entities.splice(iE, 1);
			cE--;
		}
	}
}